* Descriptive look at time series of wind plant patterns

* Individual plant names and IDs are in the public domain as part of the US Wind Turbine Database.

* make capex data
import excel using "/projects/users/########/capexCSM.xlsx", clear first 
keep State proj_name year proj_cap_mw cap_cost_dolkw csm_id
rename year p_year
keep if substr(proj_name,1,7) != "unknown"
keep if p_year >=1995 & p_year !=. & proj_cap_mw > 2
keep if State!="AK" & State!="PR" & State!="GU" & State != "HI"
sort csm_id
replace proj_cap_mw = 198 if csm_id == 700399 & proj_name=="Pomeroy I"
drop if csm_id == 700399 & proj_name=="Pomeroy II"
replace proj_cap_mw = 300.3 if csm_id == 700458 & proj_name=="Pioneer Prairie I"
drop if csm_id == 700458 & proj_name=="Pioneer Prairie II"
replace proj_cap_mw = 30 if csm_id == 700719 & proj_name=="Diamond Willow Wind"
drop if csm_id == 700719 & proj_name=="Diamond Willow extension"
replace proj_cap_mw = 289.7 if csm_id == 701326 & proj_name=="Goldthwaite (2013)"
drop if csm_id == 701326 & proj_name=="Goldthwaite"
save "/projects/users/########/capexCSM.dta", replace

* make plant data
import excel using "/projects/users/########/uswtdb_public_070318_pid_csm.xlsx", ///
	clear first sheet("Data")
	
replace p_year=. if p_year == -9999
replace p_cap=. if p_cap == -9999
replace p_tnum=. if p_tnum == -9999
replace t_cap=. if t_cap == -9999
replace t_hh=. if t_hh == -9999

preserve 
collapse p_tnum p_cap t_cap t_hh, by(p_year)

tsset p_year

* average turbine number per plant stable since mid-90s and especially early 2000s
tsline p_tnum

* average plant capacity increasing since mid-90s
tsline p_cap

* average individual turbine capacity increasing since mid-90s
tsline t_cap

* average turbine height increasing since 1989
tsline t_hh

restore

keep if substr(p_name,1,7) != "unknown"
keep if p_year >=1995 & p_year !=.
keep if t_state!="AK" & t_state!="PR" & t_state!="GU" & t_state != "HI"
keep if p_tnum <1400 & p_tnum > 7 & p_cap >2 
keep if csm_id !=0
keep if p_id !=0


* collapse to plants

* first, get rid of plants whose centroids are really far from any specific turbine.
* these are incorrectly grouped turbines - not really plants. 
preserve
collapse p_tnum p_cap t_cap t_hh xlong ylat (firstnm) p_year t_state t_county t_fips p_name, by(csm_id)
rename xlong plong
rename ylat plat
save "/projects/users/########/tempfiles/PlantsTest.dta", replace
restore 
merge m:1 csm_id using "/projects/users/########/tempfiles/PlantsTest.dta"
drop _merge
gen londist = xlong - plong
gen latdist = ylat - plat
keep if londist >-0.8 & londist < 0.8
keep if latdist >-0.8 & latdist < 0.8

preserve
collapse p_tnum p_cap t_cap t_hh xlong ylat (firstnm) p_year t_state t_county t_fips p_name, by(csm_id)
sort csm_id
merge 1:1 csm_id using "/projects/users/########/capexCSM.dta"
keep if _merge==1 | _merge ==3
drop State _merge
gen capx = cap_cost_dolkw*1000*p_cap
label var capx "Millions $"
save "/projects/users/########/PlantLevelWindDatabaseCSM.dta", replace
restore

preserve
collapse p_tnum p_cap t_cap t_hh xlong ylat (firstnm) p_year t_state t_county t_fips p_name, by(csm_id)
sort csm_id
merge 1:1 csm_id using "/projects/users/########/capexCSM.dta"
keep if _merge==1 | _merge ==3
drop State _merge
gen capx = cap_cost_dolkw*1000*p_cap
label var capx "Millions $"
keep if t_state == "AL" | t_state == "AZ" | t_state == "AR" | t_state == "CA" ///
      | t_state == "CO" | t_state == "DE" | t_state == "DC" | t_state == "IL" ///
      | t_state == "IN" | t_state == "IA" | t_state == "KS" | t_state == "ME" ///
      | t_state == "MD" | t_state == "MO" | t_state == "MT" | t_state == "NE" ///
      | t_state == "NV" | t_state == "NM" | t_state == "ND" | t_state == "OK" ///
      | t_state == "TN" | t_state == "VA" | t_state == "WY"
save "/projects/users/########/PlantLevelWindDatabaseCSM23.dta", replace
restore


